package com.source.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.sys.entity.User;
import com.source.sys.service.UserService;
import com.source.sys.vo.UserVo;
import com.source.utils.CommonResult;
import com.source.utils.ResultEnum;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZZF
 * @since 2021-02-08
 */
@Api(tags = "用户管理")
@RestController
public class UserController {
    @Resource
    private UserService userService;



    @ApiOperation(value = "获取所有用户信息(分页)")
    @GetMapping(value = "/sys/user/{currentPageNo}/{size}")
    public CommonResult<Page<User>> listAllUserPage(@ApiParam("当前页数")@PathVariable("currentPageNo") int currentPageNo,@ApiParam("每页显示多少条")@PathVariable("size") int size){
        Page<User> page = userService.getAllUserPage(currentPageNo, size);
        if(!page.getRecords().isEmpty()){
            return new CommonResult<>(ResultEnum.SUCCESS.getCode(),"查询成功",page);
        }else{
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND,page);
        }
    }

    @ApiOperation(value = "通过模糊查询获取用户信息(分页)")
    @GetMapping(value = "/sys/user/{currentPageNo}/{size}/{selectValue}")
    public CommonResult<Page<User>> listUserPageByWrapper(@ApiParam("当前页数")@PathVariable("currentPageNo") int currentPageNo, @ApiParam("每页显示多少条")@PathVariable("size") int size, @ApiParam("查询值")@PathVariable("selectValue") String selectValue){
        Page<User> userPageByWrapper = userService.getUserPageByWrapper(currentPageNo, size, selectValue);
        if(!userPageByWrapper.getRecords().isEmpty()){
            return new CommonResult<>(ResultEnum.SUCCESS.getCode(),"查询成功",userPageByWrapper);
        }else{
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND.getCode(),"没有找到匹配的记录",userPageByWrapper);
        }
    }

    @ApiOperation("增加用户信息")
    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "body",name = "user",value = "用户信息",required = true),
            @ApiImplicitParam(paramType = "form",name = "file",value = "上传二维码",required = false,dataType = "_file")
    })
    @PostMapping(value = "/sys/user")
    public CommonResult addUser(UserVo userVo,
                                @RequestPart(value = "multipartFile",required = false) MultipartFile multipartFile){
        String loginName = userVo.getLoginName();
        User user = new User();
        if(userService.getOne(new QueryWrapper<User>().eq("loginName",loginName)) != null){
            //先检验用户名是否使用
            return new CommonResult(ResultEnum.ERROR.getCode(),"添加失败，用户名被使用");
        }
        if(userVo.getLoginName() == null || userVo.getPassword() == null){
            return new CommonResult(ResultEnum.ERROR.getCode(),"添加失败，用户名和密码不能为空");
        }
        //userVo ----> user
        BeanUtils.copyProperties(userVo,user);

        boolean saveResult = userService.addUser(user, multipartFile);
        if(saveResult == true){
            return new CommonResult(ResultEnum.SUCCESS.getCode(),"添加成功");
        }else{
            return new CommonResult(ResultEnum.ERROR.getCode(),"添加失败");
        }
    }

    @ApiOperation("回显用户信息")
    @GetMapping(value = "/sys/user/{userId}")
    public CommonResult<User> showUserById(@PathVariable("userId") String userId){
        User user = userService.getById(userId);
        if(user != null){
            return new CommonResult<>(ResultEnum.SUCCESS,user);
        }else{
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
    }

    @ApiOperation("修改用户信息")
    @PutMapping(value = "/sys/user")
    public CommonResult updateUserById(String userId,String loginName,String name,String email,String mobile){
        User updateUser = userService.getById(userId);
        updateUser.setLoginName(loginName);
        updateUser.setName(name);
        updateUser.setEmail(email);
        updateUser.setMobile(mobile);
        Boolean updateUserById = userService.updateUserById(updateUser);
        if(updateUserById==true){
            return new CommonResult<>(ResultEnum.SUCCESS.getCode(),"修改成功");
        }else{
            return new CommonResult<>(ResultEnum.ERROR.getCode(),"修改失败");
        }
    }

    @ApiOperation("删除用户信息(逻辑删除)")
    @DeleteMapping(value = "/sys/user/{userId}")
    public CommonResult deleteUserById(@PathVariable("userId") String userId){
        boolean deleteUser = userService.deleteUserById(userId);
        if(deleteUser==true){
            return new CommonResult(ResultEnum.SUCCESS.getCode(),"删除成功");
        }else{
            return new CommonResult(ResultEnum.ERROR.getCode(),"删除失败");
        }
    }
}

